package com.github.ilioili.justdoit.util;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.view.View;
import android.widget.ImageView;

/**
 * Created by hantuo on 16/5/11.
 */
public class CompatUtil {
    public static Drawable getDrawable(Context context, int resId) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            return context.getResources().getDrawable(resId, context.getTheme());
        } else {
            return context.getResources().getDrawable(resId);
        }
    }

    public static void setBackground(View view, Drawable drawable) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            view.setBackground(drawable);
        } else {
            view.setBackgroundDrawable(drawable);
        }
    }

    public static void setBackground(View view, int resId) {
        setBackground(view, getDrawable(view.getContext(), resId));
    }

    public static void setFilterWithColor(ImageView imageView, int color) {
        imageView.setColorFilter(color);
    }

    public static void setFilterWithColorRes(ImageView imageView, int colorRes) {
        imageView.setColorFilter(CompatUtil.getColor(imageView.getContext(), colorRes));
    }

    public static void setFilterWithColorRes(int colorRes, ImageView... imageViews) {
        for (ImageView imageView : imageViews)
            imageView.setColorFilter(CompatUtil.getColor(imageView.getContext(), colorRes));
    }

    public static int getColor(Context context, int resId) {
        if (null == context) return Color.TRANSPARENT;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            return context.getResources().getColor(resId, context.getTheme());
        } else {
            return context.getResources().getColor(resId);
        }
    }


}
